package com.coders.layer;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.cocos2d.actions.base.CCFiniteTimeAction;
import org.cocos2d.actions.instant.CCCallFuncN;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCBezierTo;
import org.cocos2d.actions.interval.CCDelayTime;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCColorLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.nodes.CCSpriteSheet;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CCBezierConfig;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccColor3B;
import org.cocos2d.types.ccColor4B;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Handler;
import android.os.Vibrator;
import android.util.Log;
import android.view.MotionEvent;

import com.coders.R;
import com.coders.activities.GameOver;
import com.coders.activities.Play;
import com.coders.configuration.SettingsConfig;
import com.coders.info.Constants;
import com.coders.util.CommonUtil;
import com.coders.util.Logger;

/**
 * Main game layer for CWTT. Based on cocos2d android library.
 * 
 * @author Subin,Silpa
 * @param <CCTime>
 * 
 */
public class PlayLayer extends CCColorLayer {
	private static boolean isHelpShown = false;
	private static boolean pauseEnabled = true;
	private static boolean butterflyEnabled = false;

	private static boolean menuFlag = false;
	private static boolean playFlag = false;
	private static boolean FirstColor = true;
	private static boolean gameoverstaus = false;
	private Handler mHandler = new Handler();

	SQLiteDatabase db;
	int sysTime;

	private int level[] = new int[10];
	String SCORES[] = new String[] { "0", "0" };

	int VALUES[] = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int index = 0;

	boolean colorflag = false;
	int levelcount = 0;
	private List<CCSprite> popsControlColorList;
	private List<CCSprite> bunnyList;
	private List<CCSprite> paislyList;

	private List<CCSprite> popsOthersList;

	private List<CCSprite> BaloonList;

	private List<CCSprite> mOverAllBallonList;

	int temp_range;
	int lifeCounter = 3;
	private CCSprite balloonBoomSprite;
	CCSprite butterFlySprite, netSprite, heartSprite;
	CCSprite jarSprite;
	CCSprite pop_bunny_yellow, cloud, timer;
	CGPoint location;
	private int countScore = 0;

	CCLabel scoreLabel;

	CCSprite controlColor, backGround, tempControlColor, playSprite,
			pauseSprite;
	CCSprite lifeSprite1, lifeSprite2, lifeSprite3;
	boolean bunnyblasted = false;
	CCLabel timeLabel, scoreText;
	String currentLang;
	int LanguageTag;
	SoundPool soundpool;
	int soundID[] = new int[10];
	CCSpriteSheet jarSheet;
	int speedFactor = 0;
	int speedlevel = 0;
	int minPopDuration = 6;
	int maxPopDuration = 8;
	CCSprite tempjarSprite, add_balloon;
	CCSprite netWithButterfly;
	Vibrator vibrator;

	int myTime; // this
	int range = 12;
	CCDelayTime delayaction = CCDelayTime.action(3f);

	int setTime = 1;

	Random controlcolor_type, butterfly_pos;
	int pos = 0;
	CGSize winSize = CCDirector.sharedDirector().displaySize();
	Context context;
	SharedPreferences sharedPreference = CCDirector.sharedDirector()
			.getActivity().getSharedPreferences("sharedPreference", 0);

	int balloonCount = 0;
	boolean firstcolor = true;
	String[] columns;
	Boolean sethighScore = false;
	int count = 100;
	boolean waitflag = false;
	int tempvalue = 0;

	public static CCScene scene() {
		CCScene scene = CCScene.node();
		CCColorLayer layer = new PlayLayer(ccColor4B.ccc4(255, 255, 255, 255));

		scene.addChild(layer);

		return scene;
	}

	protected PlayLayer(ccColor4B color) {
		super(color);

		this.setIsTouchEnabled(true);

		FirstColor = true;
		bunnyblasted = false;
		lifeCounter = 3;
		vibrator = (Vibrator) CCDirector
				.sharedDirector()
				.getActivity()
				.getSystemService(
						CCDirector.sharedDirector().getActivity().VIBRATOR_SERVICE);

		popsControlColorList = new CopyOnWriteArrayList<CCSprite>();
		bunnyList = new CopyOnWriteArrayList<CCSprite>();
		paislyList = new CopyOnWriteArrayList<CCSprite>();
		popsOthersList = new CopyOnWriteArrayList<CCSprite>();
		BaloonList = new CopyOnWriteArrayList<CCSprite>();
		mOverAllBallonList = new CopyOnWriteArrayList<CCSprite>();
		CGSize winSize = CCDirector.sharedDirector().displaySize();
		backGround = CCSprite.sprite(CommonUtil.getImagePath(
				SettingsConfig.getBackGroundImagePath(), winSize));

		backGround
				.setScaleX(winSize.width / backGround.getTexture().getWidth());
		backGround.setScaleY(winSize.height
				/ backGround.getTexture().getHeight());
		backGround.setPosition(CGPoint.make(winSize.width / 2,
				winSize.height / 2));
		addChild(backGround);

		myTime = 50;
		lifeSprite3 = CCSprite.sprite(CommonUtil.getImagePath(
				Constants.LIFE_ICON, winSize));
		lifeSprite3.setPosition(CGPoint.ccp(
				lifeSprite3.getContentSize().width / 2, winSize.height
						- lifeSprite3.getContentSize().height / 2));

		addChild(lifeSprite3, 100);

		lifeSprite2 = CCSprite.sprite(CommonUtil.getImagePath(
				Constants.LIFE_ICON, winSize));
		lifeSprite2.setPosition(CGPoint.ccp(lifeSprite3.getContentSize().width
				+ lifeSprite2.getContentSize().width / 2, winSize.height
				- lifeSprite2.getContentSize().height / 2));

		addChild(lifeSprite2, 100);
		lifeSprite1 = CCSprite.sprite(CommonUtil.getImagePath(
				Constants.LIFE_ICON, winSize));
		lifeSprite1.setPosition(CGPoint.ccp(
				lifeSprite3.getContentSize().width
						+ lifeSprite2.getContentSize().width
						+ lifeSprite1.getContentSize().width / 2,
				winSize.height - lifeSprite2.getContentSize().height / 2));

		addChild(lifeSprite1, 100);

		scoreLabel = CCLabel.makeLabel("score:", Constants.SCORE_CARD, 30);
		scoreLabel.setColor(ccColor3B.ccBLACK);

		scoreLabel.setPosition(CGPoint.ccp(
				winSize.width - (scoreLabel.getContentSize().width),
				winSize.height - scoreLabel.getContentSize().height / 2));

		scoreLabel.setString("SCORE:" + countScore);

		level[levelcount] = 3;
		levelcount += 1;

		if (Play.isResumeGame() == true) {

		} else {

			scoreLabel.setString("SCORE:" + countScore);
			addChild(scoreLabel, 100);
		}

		this.schedule("helpScreenCheck");
	}

	@Override
	public boolean ccTouchesMoved(MotionEvent event) {

		// return handleTouchMoved(event, 0.5f);
		return super.ccTouchesMoved(event);

	}

	public void delaybunny(float dt) {
		CCDelayTime dely = CCDelayTime.action(2);
		dely.action(5f);
		this.unschedule("delaybunny");
	}

	private static final ScheduledExecutorService worker = Executors
			.newSingleThreadScheduledExecutor();

	@Override
	public boolean ccTouchesEnded(MotionEvent event) {
		return super.ccTouchesEnded(event);
	}

	public void FinishBunnyACtion(Object sender) {
		CCSprite moveFinishedPop = (CCSprite) sender;
		addBunnyBurst(location);
		moveFinishedPop.removeSelf();

	}

	private void addBunnyBurst(CGPoint touchRect) {

		CCSpriteSheet boomSheet = CCSpriteSheet.spriteSheet("baloon_burst.png");

		this.addChild(boomSheet, 100);

		// Get the first sprite, each sprite is of size 160x175px
		balloonBoomSprite = CCSprite.sprite(boomSheet.getTexture(),
				CGRect.make(0, 0, 100, 100));
		boomSheet.addChild(balloonBoomSprite, 100);
		balloonBoomSprite.setPosition(touchRect.x, touchRect.y);
		int frameCount = 0;

		ArrayList<CCSpriteFrame> boomAnimFrames = new ArrayList<CCSpriteFrame>();

		for (int y = 0; y < 1; y++) { // 3 rows of sprites
			for (int x = 0; x < 5; x++) { // each row can contain 1 sprites
				CCSpriteFrame frame = CCSpriteFrame.frame(
						boomSheet.getTexture(),
						CGRect.make(x * 100, y * 100, 100, 100),
						CGPoint.ccp(0, 0));
				// The 0,0 being the margin between two sprites

				// Adding each frame from sheet to Sprite frame
				boomAnimFrames.add(frame);
				frameCount++;
				if (frameCount == 5) // There are 3 sprites in Sprite sheet
					break;
			}
		}
		CCAnimation boomAnimation = CCAnimation.animation("ballonBurst",
				(float) 0.2, boomAnimFrames);

		CCAnimate boomAction = CCAnimate.action(boomAnimation);

		CCCallFuncN actionAnimateDone = CCCallFuncN.action(this,
				"spriteAnimationFinished");
		CCSequence actions = CCSequence.actions(boomAction, actionAnimateDone);

		balloonBoomSprite.runAction(actions);
	}

	private void addHeartBurst(CGPoint touchRect) {

		CCSpriteSheet boomSheet = CCSpriteSheet.spriteSheet("heart_anim.png");

		this.addChild(boomSheet, 100);

		balloonBoomSprite = CCSprite.sprite(boomSheet.getTexture(),
				CGRect.make(0, 0, 95, 95));
		boomSheet.addChild(balloonBoomSprite, 100);
		balloonBoomSprite.setPosition(touchRect.x, touchRect.y);
		int frameCount = 0;

		ArrayList<CCSpriteFrame> boomAnimFrames = new ArrayList<CCSpriteFrame>();

		for (int y = 0; y < 1; y++) { // 3 rows of sprites
			for (int x = 0; x < 8; x++) { // each row can contain 1 sprites
				CCSpriteFrame frame = CCSpriteFrame.frame(
						boomSheet.getTexture(),
						CGRect.make(x * 95, y * 95, 95, 95), CGPoint.ccp(0, 0));
				boomAnimFrames.add(frame);
				frameCount++;
				if (frameCount == 8) // There are 3 sprites in Sprite sheet
					break;
			}
		}
		CCAnimation boomAnimation = CCAnimation.animation("ballonBurst",
				(float) 0.2, boomAnimFrames);

		CCAnimate boomAction = CCAnimate.action(boomAnimation);

		CCCallFuncN actionAnimateDone = CCCallFuncN.action(this,
				"spriteAnimationFinished");
		CCSequence actions = CCSequence.actions(boomAction, actionAnimateDone);

		balloonBoomSprite.runAction(actions);
	}

	private void addBaloonBurst(CGPoint touchRect) {

		CCSpriteSheet boomSheet = CCSpriteSheet.spriteSheet("starburst.png");

		this.addChild(boomSheet, 100);

		balloonBoomSprite = CCSprite.sprite(boomSheet.getTexture(),
				CGRect.make(0, 0, 118, 118));
		boomSheet.addChild(balloonBoomSprite, 100);

		balloonBoomSprite.setPosition(touchRect.x, touchRect.y);
		int frameCount = 0;

		ArrayList<CCSpriteFrame> boomAnimFrames = new ArrayList<CCSpriteFrame>();

		for (int y = 0; y < 1; y++) { // 3 rows of sprites
			for (int x = 0; x < 8; x++) { // each row can contain 1 sprites
				CCSpriteFrame frame = CCSpriteFrame.frame(
						boomSheet.getTexture(),
						CGRect.make(x * 118, y * 118, 118, 118),
						CGPoint.ccp(0, 0));
				boomAnimFrames.add(frame);
				frameCount++;
				if (frameCount == 8)
					break;
			}
		}
		CCAnimation boomAnimation = CCAnimation.animation("ballonBurst",
				(float) 0.1, boomAnimFrames);

		CCAnimate boomAction = CCAnimate.action(boomAnimation);

		CCCallFuncN actionAnimateDone = CCCallFuncN.action(this,
				"spriteAnimationFinished");
		CCSequence actions = CCSequence.actions(boomAction, actionAnimateDone);

		balloonBoomSprite.runAction(actions);
	}

	public void spriteAnimationFinished(Object sender) {
		CCSprite moveFinishedBurst = (CCSprite) sender;

		moveFinishedBurst.removeSelf();

	}

	private void playWrongBaloonSound() {
		if (SettingsConfig.isSoundEnabled()) {

			boolean sound = SettingsConfig.isSoundEnabled();
			Log.v("sound enabled", String.valueOf(sound));
			SoundEngine.sharedEngine()
					.playEffect(CCDirector.sharedDirector().getActivity(),
							R.raw.devilsound);
		}
	}

	private void playPopSound() {
		if (SettingsConfig.isSoundEnabled()) {
			boolean sound = SettingsConfig.isSoundEnabled();
			Log.v("sound enabled", String.valueOf(sound));
			SoundEngine.sharedEngine().playEffect(
					CCDirector.sharedDirector().getActivity(),
					R.raw.balloonblastsound);
		}
	}

	public void helpScreenCheck(float dt) {

		if (isHelpShown) {

			Log.e("CURRENT LIFE VALUE IS", String.valueOf(lifeCounter));
			this.schedule("gameOthers", 1.3f);

			this.schedule("gameEnemey", 3f);

			this.schedule("gameliveshearts", 9f);

			this.unschedule("helpScreenCheck");

		}
	}

	public void gameOthers(float dt) {

		addOthers(range);

	}

	private int addOthers(int range2) {
		CGSize winSize = CCDirector.sharedDirector().displaySize();
		Random rand = new Random();
		int randomValue = rand.nextInt(10);

		if (randomValue == tempvalue) {

			randomValue = rand.nextInt(10);
		}

		tempvalue = randomValue;
		Random randomcolorid = new Random();
		int randomColorsValue = randomcolorid.nextInt(10);

		switch (randomValue) {
		case 1:

			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALLOON_BLUE, winSize));
			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 2:

			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALLOON_RED, winSize));

			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 3:

			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALLOON_YELLOW, winSize));
			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 4:

			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALLOON_GREEN, winSize));
			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 5:
			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALOON_PINK, winSize));

			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 6:
			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALOON_BROWN, winSize));

			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 7:
			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALOON_BLACK, winSize));

			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 8:
			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALOON_PURPLE, winSize));

			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 9:
			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALLOON_ORANGE, winSize));

			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		case 0:
			add_balloon = CCSprite.sprite(CommonUtil.getImagePath(
					Constants.BALOON_WHITE, winSize));

			addPopCOMMON_BALOONS(add_balloon, Constants.TAG_BALLOONS,
					randomColorsValue);

			break;
		default:
			break;

		}
		return range;
	}

	private void addPopCOMMON_BALOONS(CCSprite sprite, int type,
			int balloonColorId) {
		int _randomValue;
		if (count < 1) {
			count = 100;
		}
		count = count - 1;
		Random rand = new Random();

		CGSize winSize = CCDirector.sharedDirector().displaySize();
		int minX = (int) (sprite.getContentSize().width / 2.0f);
		int maxX = (int) (winSize.width - sprite.getContentSize().width / 2.0f);
		int rangeX = maxX - minX;
		int actualX = rand.nextInt(rangeX) + minX;
		sprite.setPosition(actualX, -(sprite.getContentSize().height / 2));
		addChild(sprite, count);
		if (type == 0) {
			sprite.setTag(0);

		}

		if (type == 1) {
			sprite.setTag(1);

		}
		if (type == 2) {
			sprite.setTag(2);

		}

		BaloonList.add(sprite);
		mOverAllBallonList.add(sprite);
		int randomTargetPoint = (int) winSize.width;
		int minDuration = 4;
		int maxDuration = 6;
		int rangeDuration = maxDuration - minDuration;
		int actualDuration = rand.nextInt(rangeDuration) + minDuration;
		CCMoveTo actionMove;

		switch ((balloonColorId) % 2) {
		case 0:

			CCBezierConfig config = new CCBezierConfig();
			config.controlPoint_1 = CGPoint.ccp(actualX,
					sprite.getContentSize().height / 2);
			config.controlPoint_2 = CGPoint.ccp(rand.nextInt(rangeX),
					winSize.height / 2);
			config.endPosition = CGPoint.ccp(actualX, winSize.height);

			CCBezierTo bez_actionwithduration = CCBezierTo.action(
					actualDuration, config);

			CCCallFuncN bez_actionMoveDone = CCCallFuncN.action(this,
					"spriteOthersFinished");
			CCSequence bez_actions = CCSequence.actions(bez_actionwithduration,
					bez_actionMoveDone);
			sprite.runAction(bez_actions);

			break;

		case 1:

			CCFiniteTimeAction actionwithduration = CCMoveTo.action(
					actualDuration, CGPoint.ccp(actualX, winSize.height));
			CCCallFuncN actionMoveDone = CCCallFuncN.action(this,
					"spriteOthersFinished");
			CCSequence actions = CCSequence.actions(actionwithduration,
					actionMoveDone);
			sprite.runAction(actions);

			break;

		default:

			CCFiniteTimeAction def_actionwithduration = CCMoveTo.action(
					actualDuration, CGPoint.ccp(actualX, winSize.height));
			CCCallFuncN def_actionMoveDone = CCCallFuncN.action(this,
					"spriteOthersFinished");
			CCSequence def_actions = CCSequence.actions(def_actionwithduration,
					def_actionMoveDone);
			sprite.runAction(def_actions);

		}

	}

	public void spriteOthersFinished(Object sender) {
		CCSprite moveFinishedPop = (CCSprite) sender;
		BaloonList.remove(moveFinishedPop);
		mOverAllBallonList.remove(moveFinishedPop);

		moveFinishedPop.removeSelf();

	}

	private void addHeartPop(CCSprite sprite, int type) {
		if (count < 1) {
			count = 100;
		}
		count = count - 1;
		Random rand = new Random();

		// Determine where to spawn the pop along the X axis
		CGSize winSize = CCDirector.sharedDirector().displaySize();
		int minX = (int) (sprite.getContentSize().width / 2.0f);
		int maxX = (int) (winSize.width - sprite.getContentSize().width / 2.0f);
		int rangeX = maxX - minX;
		int actualX = rand.nextInt(rangeX) + minX;

		// Create the pop slightly off-screen along the bottom edge,
		// and along a random position along the X axis as calculated above
		// sprite.setPosition( actualX, -( winSize.height -
		// sprite.getContentSize().height ) );
		sprite.setPosition(actualX, -(sprite.getContentSize().height / 2));
		// addChild( pop );
		addChild(sprite, count);
		if (type == 0) {
			sprite.setTag(0);

		}

		if (type == 1) {
			sprite.setTag(1);

		}
		if (type == 2) {
			sprite.setTag(2);

		}

		BaloonList.add(sprite);
		mOverAllBallonList.add(sprite);
		// Created randomTargetPoint variable for the random movement of
		// balloons
		int randomTargetPoint = (int) winSize.width;

		// Determine speed of the pop
		int minDuration = 3;
		int maxDuration = 5;
		int rangeDuration = maxDuration - minDuration;
		int actualDuration = rand.nextInt(rangeDuration) + minDuration;
		CCMoveTo actionMove;

		CCBezierConfig config = new CCBezierConfig();
		config.controlPoint_1 = CGPoint.ccp(actualX,
				sprite.getContentSize().height / 2);
		config.controlPoint_2 = CGPoint.ccp(rand.nextInt(rangeX),
				winSize.height / 2);
		config.endPosition = CGPoint.ccp(actualX, winSize.height);

		// Create the curve actions

		CCBezierTo actionwithduration = CCBezierTo.action(2.5f, config);

		CCCallFuncN actionMoveDone = CCCallFuncN.action(this,
				"spriteHeartFinished");
		CCSequence actions = CCSequence.actions(actionwithduration,
				actionMoveDone);

		sprite.runAction(actions);

	}

	public void spriteHeartFinished(Object sender) {
		CCSprite moveFinishedPop = (CCSprite) sender;
		BaloonList.remove(moveFinishedPop);
		mOverAllBallonList.remove(moveFinishedPop);
		moveFinishedPop.removeSelf();

	}

	public void gameliveshearts(float dt) {

		addHeart(range);
	}

	public void gameEnemey(float dt) {

		addEnemey(range);
	}

	public void gameHeart(float dt) {

		addHeart(range);
	}

	private void addHeart(int range) {
		winSize = CCDirector.sharedDirector().displaySize();
		CCSprite ballon = CCSprite.sprite(CommonUtil.getImagePath(
				Constants.LIFE_ICON_GAME, winSize));

		addHeartPop(ballon, Constants.TAG_HEARTS);

	}

	private void addEnemey(int range) {
		Random randomcolorid = new Random();
		int randomValue = randomcolorid.nextInt(10);

		winSize = CCDirector.sharedDirector().displaySize();
		CCSprite ballon = CCSprite.sprite(CommonUtil.getImagePath(
				Constants.PAISLY_BLACK, winSize));

		addPopCOMMON_BALOONS(ballon, Constants.TAG_ENEMY, randomValue);

	}

	@Override
	public boolean ccTouchesBegan(MotionEvent event) {

		boolean isballontouch = false;
		location = CCDirector.sharedDirector().convertToGL(
				CGPoint.ccp(event.getX(), event.getY()));

		for (int i = 0; i < mOverAllBallonList.size(); i++) {

			CCSprite ballons = mOverAllBallonList.get(i);
			CGRect popRect = ballons.getBoundingBox();
			location = CCDirector.sharedDirector().convertToGL(
					CGPoint.ccp(event.getX(), event.getY()));

			if (popRect.containsPoint(popRect, location) && (!isballontouch)) {
				if (ballons.getTag() == 2) {
					mOverAllBallonList.remove(ballons);
					ballons.removeSelf();
					addHeartBurst(location);
					countScore += 100;
					playheartSound();
					if (lifeCounter < 3 && lifeCounter > 0) {
						lifeCounter = lifeCounter + 1;

						switch (lifeCounter) {
						case 2:
							lifeSprite1.setVisible(true);
							lifeSprite2.setVisible(true);
							lifeSprite3.setVisible(false);
							break;
						case 3:
							lifeSprite1.setVisible(true);
							lifeSprite2.setVisible(true);
							lifeSprite3.setVisible(true);
							break;

						default:

						}
						if (lifeCounter > 3) {
							lifeSprite1.setVisible(true);
							lifeSprite2.setVisible(true);
							lifeSprite3.setVisible(true);

						}

					}
					Log.e("lifeCounter value is in heart",
							String.valueOf(lifeCounter));
					vibrator.vibrate(100);
					if (countScore < 0) {
						countScore = 0;
					} else {
						scoreLabel.setString("SCORE:" + countScore);
					}
				}
			}
			if (popRect.containsPoint(popRect, location) && (!isballontouch)) {

				if (ballons.getTag() == 1) {
					mOverAllBallonList.remove(ballons);
					ballons.removeSelf();
					addBunnyBurst(location);
					playWrongBaloonSound();
					countScore -= 10;
					if (lifeCounter > 0) {
						lifeCounter--;

						switch (lifeCounter) {
						case 1:
							lifeSprite1.setVisible(true);
							lifeSprite2.setVisible(false);
							lifeSprite3.setVisible(false);

							break;
						case 2:
							lifeSprite1.setVisible(true);
							lifeSprite2.setVisible(true);
							lifeSprite3.setVisible(false);

							break;
						case 3:
							lifeSprite1.setVisible(true);
							lifeSprite2.setVisible(true);
							lifeSprite3.setVisible(true);
							break;

						default:

						}
					}
					if (lifeCounter == 0) {
						lifeSprite1.setVisible(false);
						lifeSprite2.setVisible(false);
						lifeSprite3.setVisible(false);

						Intent mainScore = new Intent(CCDirector
								.sharedDirector().getActivity(), GameOver.class);
						mainScore.putExtra("totalscore", countScore);
						CCDirector.sharedDirector().getActivity()
								.startActivity(mainScore);
						CCDirector.sharedDirector().getActivity().finish();

					}

					Log.e("lifeCounter value is in enemey",
							String.valueOf(lifeCounter));
					vibrator.vibrate(100);
					if (countScore < 0) {
						countScore = 0;
					} else {
						scoreLabel.setString("SCORE:" + countScore);
					}
					balloonCount += 1;
				} else {
					if (ballons.getTag() == 0) {
						isballontouch = true;
						mOverAllBallonList.remove(ballons);
						ballons.removeSelf();

						addBaloonBurst(location);

						countScore += 10;
						vibrator.vibrate(100);
						scoreLabel.setString("SCORE:" + countScore);
						balloonCount += 1;
						if (SettingsConfig.isSoundEnabled()) {
							playPopSound();
						}

					}
				}

			}

		}

		return super.ccTouchesBegan(event);
	}

	private void playheartSound() {
		if (SettingsConfig.isSoundEnabled()) {

			boolean sound = SettingsConfig.isSoundEnabled();
			Log.v("sound enabled", String.valueOf(sound));
			SoundEngine.sharedEngine().playEffect(
					CCDirector.sharedDirector().getActivity(),
					R.raw.paisleyballoon);
		}
	}

	public void spriteMoveFinished(Object sender) {
		CCSprite moveFinishedPop = (CCSprite) sender;
		popsControlColorList.remove(moveFinishedPop);
		moveFinishedPop.removeSelf();

	}

	public static boolean isHelpShown() {
		return isHelpShown;
	}

	public static void setHelpShown(boolean isHelpShown) {
		PlayLayer.isHelpShown = isHelpShown;
	}

	public static void main(String[] args) {
		FirstColor = false;
	}

}
